package com.yunkuhui.www.demo.configuration;

import com.yunkuhui.www.demo.conf.MysqlConf;
import org.apache.ibatis.session.SqlSessionFactory;
import org.jetbrains.annotations.NotNull;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;


@Configuration
@MapperScan(basePackages = {"com.yunkuhui.www.demo.dao.test"}, sqlSessionFactoryRef = "testSqlSessionFactory")
public class MysqlTestConfiguration {

	private MysqlConf mysqlConf;

	@Autowired
	public MysqlTestConfiguration(@NotNull MysqlConf mysqlConf) {
		this.mysqlConf = mysqlConf.getMysqlConf().get("test");
	}

	@Primary
	@Bean(name = "testDataSource")
	public DataSource dataSource() throws SQLException {
		return mysqlConf.getDataSource();
	}

	@Primary
	@Bean(name = "testSqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		return mysqlConf.getSqlSessionFactory();
	}

	@Primary
	@Bean(name = "testDataSourceTransactionManager")
	public DataSourceTransactionManager dataSourceTransactionManager() throws SQLException {
		return new DataSourceTransactionManager(mysqlConf.getDataSource());
	}
}
